#include "rs485.h"

void RS485_Config(void)
{
	/* enable USART and GPIOA clock */
	rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_USART0);
	
	/* configure USART0 Tx as alternate function push-pull */
    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
	/* configure USART0 Rx as alternate function floating */
	gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
	
	
	/* USART0 and USART1 baudrate configuration */
    usart_baudrate_set(USART0, 9600U);
	
	/* USART configure */
    usart_word_length_set(USART0, USART_WL_8BIT);
    usart_stop_bit_set(USART0, USART_STB_1BIT);
    usart_parity_config(USART0, USART_PM_NONE);
    usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
    usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
	
	/* configure USART receiver */
    usart_receive_config(USART0, USART_RECEIVE_ENABLE);
	
	/* configure USART transmitter */
    usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
	
//	/* enable USART0 half duplex mode*/
//    usart_halfduplex_enable(USART0);

	/* enable USART */
    usart_enable(USART0);
	
	/*enable NVIC request*/
	nvic_irq_enable(USART0_IRQn,0,0);
	usart_interrupt_enable(USART0,USART_INT_TC);
}